package com.xinbochuang.machine.common.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黄晓鹏
 * @date 2021-05-23 21:15
 */
public class CustomDateUtils {

    public static void main(String[] args) {
        System.out.println(getThisWeekList());
    }

    /**
     * 获取本周周一日期
     */
    public static String getFirstDayByWeek() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return sdf.format(calendar.getTime());
    }

    /**
     * 根据周一获取周一到周日日期
     */
    public static List<String> getWeekList(String monday) {
        List<String> result = new ArrayList<>();
        result.add(monday);
        Date date = DateUtil.parse(monday);
        for (int i = 1; i <= 6; i++) {
            DateTime newDate = DateUtil.offsetDay(date, i);
            result.add(DateUtil.format(newDate, DatePattern.NORM_DATE_PATTERN));
        }
        return result;
    }

    /**
     * 获取本周周一到周日日期
     */
    public static List<String> getThisWeekList() {
        return getWeekList(getFirstDayByWeek());
    }

}
